package edu.gdpu.jmm;

/**
 * @author mazebin
 * @date 2021年 07月15日 22:39:24
 * 参考链接：
 * https://www.cnblogs.com/z00377750/p/9180644.html
 */
public class MESICacheLinePaddingTest2 {
    private static class Padding {
        public volatile long p1, p2, p3, p4, p5, p6, p7;
    }
    private static  final  int count = 1000_0000;
    private static class T extends Padding{
        public volatile  long x=0L;
    }
    private static T[] arr = new T[2];
    //arr[0]和arr[1]在同一个缓存行中概率非常大
    static {
        arr[0] = new T();
        arr[1] = new T();

    }
    public static void main(String[] args) throws Exception {
        test();
    }

    private static void test() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (long i = 0; i < count; i++) {
                arr[0].x = i;
            }
        });

        Thread t2 = new Thread(() -> {
            for (long i = 0; i < count; i++) {
                arr[1].x = i;
            }
        });

        final long start = System.nanoTime();
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println((System.nanoTime() - start) / 100_0000);//inter i5-6200U 8G 时间： 156 117 139
    }
}
